package enjoyhiking.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class FILE 
{
	public static final int        BUFFER_SIZE     = 2048;

	
	public enum MODE
	{
		APPEND,
		NEW
	}
	
	static void writeStringToFileAtSD(String path, String name, String text, MODE mode )
	{
		/* make path */
		File file = new File(path);

		if (!file.exists()) {
			file.mkdirs();
		}

		/* make file */
		try {
			file = new File(path, name);
		} catch (NullPointerException e) {
			e.printStackTrace();
			return;
		}

		if (!file.canRead()) 
		{
			try 
			{
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		FileWriter writer = null;
		BufferedWriter bw = null;

		try 
		{
			if (mode == FILE.MODE.NEW) 
			{
				writer = new FileWriter(file, false);
				bw = new BufferedWriter(writer, BUFFER_SIZE);
			} 
			else if (mode == FILE.MODE.APPEND) 
			{
				writer = new FileWriter(file, true);
				bw = new BufferedWriter(writer, BUFFER_SIZE);
			}

			bw.write(text);

		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		} 
		finally 
		{
			try 
			{
				bw.close();
			} 
			catch (IOException e) 
			{
				e.printStackTrace();
			}
		}

		return;
	}
}
